{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Some Typical Financial Calculations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Packages\n",
    "\n",
    "The [Roots](https://github.com/JuliaMath/Roots.jl) package solves non-linear equations and the [StatsBase](https://github.com/JuliaStats/StatsBase.jl) package has methods for estimating autocorrelations etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "printTable2"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Dates, DelimitedFiles, LinearAlgebra, Roots, Distributions, StatsBase\n",
    "\n",
    "include(\"printmat.jl\")\n",
    "include(\"printTable.jl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Plots\n",
    "\n",
    "#pyplot(size=(600,400))        #use pyplot or gr\n",
    "gr(size=(480,320))\n",
    "default(fmt = :svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1979-01-01\n",
      "1979-02-01\n",
      "1979-03-01\n",
      "1979-04-01\n",
      "\n"
     ]
    }
   ],
   "source": [
    "x   = readdlm(\"Data/MyData.csv\",',',skipstart=1)        #monthly return data\n",
    "ym  = round.(Int,x[:,1])     #yearmonth, like 200712\n",
    "Rme = x[:,2]                 #market excess return\n",
    "Rf  = x[:,3]                 #interest rate\n",
    "R   = x[:,4]                 #return small growth stocks\n",
    "Re  = R - Rf                 #excess returns\n",
    "T   = size(Rme,1)\n",
    "\n",
    "dN = Date.(string.(ym),\"yyyymm\")  #convert to string and then Julia Date\n",
    "printmat(dN[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CAPM\n",
    "\n",
    "The CAPM regression is\n",
    "\n",
    "$R_{it}^{e}  =\\alpha_{i}+\\beta_{i}R_{mt}^{e}+\\varepsilon_{it}$,\n",
    "\n",
    "where $R_{it}^{e}$ is the excess return of asset $i$ and $R_{mt}^{e}$ is the market excess return. Theory says that $\\alpha=0$, which is easily tested."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      coeff       std    t-stat\n",
      "α    -0.504     0.304    -1.656\n",
      "β     1.341     0.066    20.427\n",
      "\n",
      "      R2:      0.519\n",
      "no. of observations:        388\n"
     ]
    }
   ],
   "source": [
    "x    = [ones(T) Rme]             #regressors\n",
    "y    = copy(Re)                  #to get standard OLS notation\n",
    "b    = x\\y                       #OLS\n",
    "u    = y - x*b                   #residuals\n",
    "covb = inv(x'x)*var(u)           #cov(b), see any textbook\n",
    "stdb = sqrt.(diag(covb))         #std(b)\n",
    "R2a  = 1 - var(u)/var(y)\n",
    "\n",
    "printTable([b stdb b./stdb],[\"coeff\",\"std\",\"t-stat\"],[\"α\",\"β\"])\n",
    "printlnPs(\"R2: \",R2a)\n",
    "printlnPs(\"no. of observations: \",T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Return Autocorrelation\n",
    "\n",
    "That is, the correlation of $R_{t}^{e}$ and $R_{t-s}^{e}$. \n",
    "\n",
    "It can be shown that the t-stat of an autocorrelation is $\\sqrt{T}$ times the autocorrelation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Autocorrelations (different lags) of the excess returns in Re\n",
      "lag  autocorr    t-stat\n",
      "1       0.216     4.253\n",
      "2       0.002     0.046\n",
      "3      -0.018    -0.359\n",
      "4      -0.065    -1.289\n",
      "5      -0.027    -0.536\n",
      "\n"
     ]
    }
   ],
   "source": [
    "plags = 1:5\n",
    "xCorr = autocor(Re,plags)         #using the StatsBase package\n",
    "\n",
    "println(\"Autocorrelations (different lags) of the excess returns in Re\")\n",
    "printTable([xCorr sqrt(T)*xCorr],[\"autocorr\",\"t-stat\"],string.(plags),cell00=\"lag\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Trading Strategy\n",
    "\n",
    "The next cell implements a very simple momentum trading strategy. \n",
    "\n",
    "1. If $R_{t-1}^{e}\\ge0$, then we hold the market index and shorten the riskfree from $t-1$ to $t$. This means that we will earn $R_{t}^{e}$.\n",
    "\n",
    "2. Instead,  if $R_{t-1}^{e}<0$, then we do the opposite. This means that we will earn $-R_{t}^{e}$. \n",
    "\n",
    "This simple strategy could be coded without using a loop, but \"vectorization\" does not speed up much. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The annualized mean excess return of the strategy and a passive portfolio are:     26.733     3.329\n",
      "The annualized Sharpe ratios are:      0.932     0.112\n"
     ]
    }
   ],
   "source": [
    "(w,Rp)  = (fill(NaN,T),fill(NaN,T))\n",
    "for t = 2:T\n",
    "  w[t]  = (Re[t-1] < 0)*(-1) + (Re[t-1] >= 0)*1       #w is -1 or 1\n",
    "  Rp[t] = w[t]*Re[t] \n",
    "end\n",
    "\n",
    "μ = [mean(Rp[2:end]) mean(Re[2:end])]\n",
    "σ = [std(Rp[2:end])  std(Re[2:end])]\n",
    "\n",
    "printlnPs(\"The annualized mean excess return of the strategy and a passive portfolio are: \",μ*12)\n",
    "printlnPs(\"The annualized Sharpe ratios are: \",sqrt(12)*μ./σ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Value at Risk\n",
    "\n",
    "The next cell constructs an simple estimate of $\\sigma_t^2$ as a backward looking moving average (the RiskMetrics approach):\n",
    "\n",
    "$\\sigma_t^2 = \\lambda \\sigma_{t-1}^2 + (1-\\lambda) (R_{t-1} -\\mu)^2$,\n",
    "where $\\mu$ is the average return (for all data).\n",
    "\n",
    "Then, we calculate the 95% VaR by assuming a $N(\\mu,\\sigma_t^2)$ distribution:\n",
    "\n",
    "$\\textrm{VaR}_{t} = - (\\mu-1.64\\sigma_t)$.\n",
    "\n",
    "If the model is correct, then $-R_t > \\text{VaR}_{t}$ should only happen 5% of the times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n"
     ]
    }
   ],
   "source": [
    "μ = mean(Rme)\n",
    "\n",
    "λ = 0.95                         #weight on old volatility\n",
    "σ² = fill(var(Rme),T)            #RiskMetrics approach to estimate variance\n",
    "for t = 2:T\n",
    "  σ²[t] = λ*σ²[t-1] + (1-λ)*(Rme[t-1]-μ)^2\n",
    "end\n",
    "\n",
    "VaR95 = -(μ .- 1.64*sqrt.(σ²))      #VaR at 95% level\n",
    "println(\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip4100\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4100)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4101\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4100)\" d=\"\n",
       "M242.516 1167.47 L1872.76 1167.47 L1872.76 121.675 L242.516 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4102\">\n",
       "    <rect x=\"242\" y=\"121\" width=\"1631\" height=\"1047\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  336.317,1167.47 336.317,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  813.322,1167.47 813.322,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1290.2,1167.47 1290.2,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1767.2,1167.47 1767.2,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.516,1167.47 1872.76,1167.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.516,929.793 1872.76,929.793 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.516,692.111 1872.76,692.111 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.516,454.43 1872.76,454.43 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.516,216.748 1872.76,216.748 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,1167.47 1872.76,1167.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,1167.47 242.516,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  336.317,1167.47 336.317,1154.93 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  813.322,1167.47 813.322,1154.93 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1290.2,1167.47 1290.2,1154.93 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1767.2,1167.47 1767.2,1154.93 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,1167.47 262.079,1167.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,929.793 262.079,929.793 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,692.111 262.079,692.111 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,454.43 262.079,454.43 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.516,216.748 262.079,216.748 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 336.317, 1219.07)\" x=\"336.317\" y=\"1219.07\">1980</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 813.322, 1219.07)\" x=\"813.322\" y=\"1219.07\">1990</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1290.2, 1219.07)\" x=\"1290.2\" y=\"1219.07\">2000</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1767.2, 1219.07)\" x=\"1767.2\" y=\"1219.07\">2010</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 223.316, 1184.97)\" x=\"223.316\" y=\"1184.97\">0.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 223.316, 947.293)\" x=\"223.316\" y=\"947.293\">2.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 223.316, 709.611)\" x=\"223.316\" y=\"709.611\">5.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 223.316, 471.93)\" x=\"223.316\" y=\"471.93\">7.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 223.316, 234.248)\" x=\"223.316\" y=\"234.248\">10.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1057.64, 73.2)\" x=\"1057.64\" y=\"73.2\">1-month Value at Risk (95%)</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 98.8861, 644.575)\" x=\"98.8861\" y=\"644.575\">%</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip4102)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  288.655,506.844 292.703,513.986 296.359,518.001 300.407,512.894 304.325,530.65 308.373,541.307 312.29,548.876 316.338,565.973 320.386,558.503 324.303,573.812 \n",
       "  328.351,520.878 332.269,518.844 336.317,535.297 340.365,529.104 344.151,544.983 348.199,405.265 352.117,417.427 356.165,421.702 360.082,436.969 364.13,430.66 \n",
       "  368.178,449.784 372.095,467.353 376.143,486.357 380.061,440.479 384.109,437.89 388.157,432.901 391.813,452.938 395.861,466.135 399.778,478.871 403.826,497.628 \n",
       "  407.744,508.503 411.791,522.617 415.839,491.989 419.757,455.16 423.805,460.43 427.722,472.914 431.77,476.896 435.818,482.466 439.474,465.208 443.522,478.943 \n",
       "  447.44,492.204 451.488,493.851 455.405,499.154 459.453,505.844 463.501,433.559 467.418,453.336 471.466,385.896 475.384,395.571 479.431,416.541 483.479,430.55 \n",
       "  487.136,448.123 491.184,463.775 495.101,453.073 499.149,472.611 503.066,486.457 507.114,488.23 511.162,506.011 515.08,524.155 519.128,526.646 523.045,541.869 \n",
       "  527.093,553.998 531.141,564.426 534.928,555.888 538.976,572.823 542.893,588.038 546.941,562.725 550.858,578.567 554.906,583.334 558.954,511.655 562.872,527.944 \n",
       "  566.92,543.265 570.837,555.261 574.885,571.027 578.933,538.389 582.589,555.532 586.637,570.673 590.554,584.971 594.602,583.251 598.52,599.19 602.568,613.468 \n",
       "  606.616,626.234 610.533,614.051 614.581,619.222 618.498,601.901 622.546,608.378 626.594,623.95 630.25,601.519 634.298,599.993 638.216,612.173 642.264,611.705 \n",
       "  646.181,627.136 650.229,591.957 654.277,578.25 658.194,521.556 662.242,526.217 666.16,543.663 670.208,548.276 674.256,447.055 677.912,455.503 681.96,473.615 \n",
       "  685.877,486.418 689.925,504.923 693.843,513.844 697.89,522.02 701.938,533.664 705.856,542.368 709.904,162.875 713.821,151.168 717.869,157.383 721.917,176.872 \n",
       "  725.704,193.409 729.752,215.117 733.669,240.68 737.717,264.868 741.635,278.533 745.682,300.257 749.73,312.979 753.648,331.806 757.696,354.205 761.613,370.602 \n",
       "  765.661,391.665 769.709,390.756 773.365,405.813 777.413,425.961 781.331,436.418 785.379,451.299 789.296,468.269 793.344,454.314 797.392,473.211 801.309,490.61 \n",
       "  805.357,494.284 809.275,512.575 813.322,530.272 817.37,490.199 821.027,508.711 825.075,525.653 828.992,528.363 833.04,496.002 836.957,512.122 841.005,525.858 \n",
       "  845.053,452.204 848.971,437.572 853.019,452.44 856.936,448.822 860.984,466.016 865.032,473.524 868.688,458.294 872.736,474.926 876.653,493.376 880.701,504.27 \n",
       "  884.619,497.512 888.667,504.97 892.715,520.929 896.632,534.62 900.68,551.574 904.597,548.279 908.645,483.758 912.693,501.571 916.48,519.7 920.528,527.917 \n",
       "  924.445,545.403 928.493,562.55 932.411,571.701 936.459,579.251 940.507,587.288 944.424,603.288 948.472,618.95 952.389,623.916 956.437,638.291 960.485,652.945 \n",
       "  964.141,667.335 968.189,678.381 972.107,679.316 976.155,687.924 980.072,701.402 984.12,713.641 988.168,715.004 992.085,727.124 996.133,738.501 1000.05,742.141 \n",
       "  1004.1,752.747 1008.15,757.771 1011.8,755.853 1015.85,729.769 1019.77,742.293 1023.82,754.508 1027.73,748.586 1031.78,754.469 1035.83,752.256 1039.75,753.898 \n",
       "  1043.79,765.529 1047.71,748.197 1051.76,760.2 1055.81,770.572 1059.46,770.214 1063.51,778.055 1067.43,786.406 1071.48,790.31 1075.39,795.33 1079.44,793.09 \n",
       "  1083.49,803.99 1087.41,804.681 1091.46,808.181 1095.37,803.228 1099.42,813.629 1103.47,819.27 1107.26,828.91 1111.3,838.916 1115.22,845.122 1119.27,850.244 \n",
       "  1123.19,854.179 1127.23,799.603 1131.28,803.082 1135.2,787.764 1139.25,798.654 1143.17,766.6 1147.21,771.648 1151.26,758.385 1154.92,768.572 1158.97,740.227 \n",
       "  1162.88,739.428 1166.93,706.556 1170.85,705.645 1174.9,668.654 1178.94,659.086 1182.86,648.484 1186.91,641.928 1190.83,652.213 1194.87,666.178 1198.92,679.942 \n",
       "  1202.58,649.588 1206.63,645.881 1210.54,660.534 1214.59,660.893 1218.51,669.947 1222.56,671.583 1226.61,428.331 1230.52,426.658 1234.57,414.347 1238.49,413.633 \n",
       "  1242.54,412.637 1246.58,426.776 1250.24,429.456 1254.29,443.65 1258.21,451.144 1262.25,463.454 1266.17,469.229 1270.22,474.929 1274.27,490.621 1278.18,500.172 \n",
       "  1282.23,495.459 1286.15,507.634 1290.2,480.436 1294.25,478.858 1298.03,493.896 1302.08,496.951 1306,474.432 1310.05,472.923 1313.96,477.755 1318.01,490.19 \n",
       "  1322.06,473.901 1325.98,461.437 1330.02,470.122 1333.94,389.334 1337.99,409.83 1342.04,424.451 1345.69,356.727 1349.74,333.124 1353.66,318.327 1357.71,341.263 \n",
       "  1361.62,358.757 1365.67,375.326 1369.72,363.441 1373.64,315.299 1377.69,335.7 1381.6,323.467 1385.65,345.542 1389.7,363.921 1393.36,379.638 1397.4,390.788 \n",
       "  1401.32,387.863 1405.37,406.663 1409.29,382.704 1413.33,347.791 1417.38,369.992 1421.3,311.322 1425.35,303.886 1429.26,307.609 1433.31,306.345 1437.36,323.337 \n",
       "  1441.02,342.721 1445.06,364.977 1448.98,346.08 1453.03,345.9 1456.95,367.54 1461,387.294 1465.04,406.013 1468.96,424.815 1473.01,423 1476.93,442.54 \n",
       "  1480.97,450.508 1485.02,467.979 1488.81,486.49 1492.86,502.565 1496.77,512.59 1500.82,530.131 1504.74,545.759 1508.79,544.831 1512.84,561.867 1516.75,576.968 \n",
       "  1520.8,592.271 1524.72,592.178 1528.77,600.739 1532.81,604.947 1536.47,618.355 1540.52,627.305 1544.44,630.957 1548.48,636.873 1552.4,651.65 1556.45,653.073 \n",
       "  1560.5,665.125 1564.41,679.262 1568.46,683.202 1572.38,685.763 1576.43,699.031 1580.48,701.436 1584.13,713.24 1588.18,725.119 1592.1,737.626 1596.14,728.583 \n",
       "  1600.06,739.783 1604.11,750.238 1608.16,759.35 1612.08,769.98 1616.12,771.621 1620.04,780.599 1624.09,791.835 1628.14,801.635 1631.79,804.068 1635.84,814.612 \n",
       "  1639.76,811.992 1643.81,810.115 1647.72,811.524 1651.77,796.524 1655.82,807.334 1659.74,803.173 1663.78,809.799 1667.7,771.437 1671.75,780.589 1675.8,727.056 \n",
       "  1679.58,729.002 1683.63,737.441 1687.55,726.277 1691.6,735.672 1695.52,660.998 1699.56,670.543 1703.61,684.405 1707.53,581.911 1711.58,309.072 1715.49,276.814 \n",
       "  1719.54,299.417 1723.59,277.003 1727.25,228.226 1731.29,212.763 1735.21,173.39 1739.26,177.972 1743.18,204.013 1747.22,194.922 1751.27,216.98 1755.19,233.042 \n",
       "  1759.24,250.73 1763.16,258.637 1767.2,279.666 1771.25,291.408 1774.91,309.329 1778.96,309.583 1782.87,331.385 1786.92,303.802 1790.84,304.532 1794.89,299.659 \n",
       "  1798.93,306.371 1802.85,280.316 1806.9,297.16 1810.82,320.644 1814.86,317.674 1818.91,339.278 1822.57,354.299 1826.62,376.264 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip4100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 431.77, 1089.9)\" x=\"431.77\" y=\"1089.9\">(for US equity market)</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xTicksLoc = Dates.value.([Date(1980);Date(1990);Date(2000);Date(2010)])\n",
    "xTicksLab = [\"1980\";\"1990\";\"2000\";\"2010\"]   #crude way of getting tick marks right\n",
    "\n",
    "plot( dN,VaR95,\n",
    "      color = :blue,\n",
    "      legend = false,\n",
    "      xticks = (xTicksLoc,xTicksLab),\n",
    "      ylim = (0,11),\n",
    "      title = \"1-month Value at Risk (95%)\",\n",
    "      ylabel = \"%\",\n",
    "      annotation = (Dates.value(Date(1982)),1,text(\"(for US equity market)\",8,:left)) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Options"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Black-Scholes Option Price\n",
    "\n",
    "Let $S$ be the the current spot price of an asset and $y$ be the interest rate.\n",
    "\n",
    "The Black-Scholes formula for a European call option with strike price $K$ and time to expiration $m$ is\n",
    "\n",
    "$C  =S\\Phi(d_{1})  -e^{-ym}K\\Phi(d_{2})$, where\n",
    "\n",
    "$d_{1} =\\frac{\\ln(S/K)+(y+\\sigma^{2}/2)m}{\\sigma\\sqrt{m}} \\ \\text{ and } \\ d_{2}=d_{1}-\\sigma\\sqrt{m}$ \n",
    "\n",
    "and where $\\Phi(d)$ denotes the probability of $x\\leq d$ when $x$ has an $N(0,1)$ distribution. All variables except the volatility ($\\sigma$) are directly observable. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OptionBlackSPs"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Φ(x) = cdf(Normal(0,1),x)     #a one-line function, Pr(z<=x) for N(0,1)\n",
    "\n",
    "\"\"\"\n",
    "Calculate Black-Scholes european call option price\n",
    "\"\"\"\n",
    "function OptionBlackSPs(S,K,m,y,σ)\n",
    "\n",
    "  d1 = ( log(S/K) + (y+1/2*σ^2)*m ) / (σ*sqrt(m))\n",
    "  d2 = d1 - σ*sqrt(m)\n",
    "  c  = S*Φ(d1) - exp(-y*m)*K*Φ(d2)\n",
    "  return c\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         \n",
      "call price according to Black-Scholes:      1.358\n"
     ]
    }
   ],
   "source": [
    "σ = 0.4\n",
    "c1 = OptionBlackSPs(10,10,0.5,0.1,σ)\n",
    "printlnPs(\"\\n\",\"call price according to Black-Scholes: \",c1)\n",
    "\n",
    "K = range(7,stop=13,length=51)\n",
    "c = OptionBlackSPs.(10,K,0.5,0.1,σ);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip4500\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4500)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4501\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4500)\" d=\"\n",
       "M210.954 1108.82 L1872.76 1108.82 L1872.76 121.675 L210.954 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4502\">\n",
       "    <rect x=\"210\" y=\"121\" width=\"1663\" height=\"988\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  257.986,1108.82 257.986,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  519.276,1108.82 519.276,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  780.565,1108.82 780.565,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1041.85,1108.82 1041.85,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1303.14,1108.82 1303.14,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1564.43,1108.82 1564.43,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1825.72,1108.82 1825.72,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,1052.9 1872.76,1052.9 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,898.159 1872.76,898.159 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,743.415 1872.76,743.415 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,588.672 1872.76,588.672 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,433.928 1872.76,433.928 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,279.185 1872.76,279.185 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  210.954,124.441 1872.76,124.441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,1108.82 210.954,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  257.986,1108.82 257.986,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  519.276,1108.82 519.276,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  780.565,1108.82 780.565,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1041.85,1108.82 1041.85,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1303.14,1108.82 1303.14,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1564.43,1108.82 1564.43,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1825.72,1108.82 1825.72,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,1052.9 230.896,1052.9 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,898.159 230.896,898.159 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,743.415 230.896,743.415 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,588.672 230.896,588.672 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,433.928 230.896,433.928 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,279.185 230.896,279.185 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4500)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  210.954,124.441 230.896,124.441 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 257.986, 1160.42)\" x=\"257.986\" y=\"1160.42\">7</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 519.276, 1160.42)\" x=\"519.276\" y=\"1160.42\">8</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 780.565, 1160.42)\" x=\"780.565\" y=\"1160.42\">9</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1041.85, 1160.42)\" x=\"1041.85\" y=\"1160.42\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1303.14, 1160.42)\" x=\"1303.14\" y=\"1160.42\">11</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1564.43, 1160.42)\" x=\"1564.43\" y=\"1160.42\">12</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1825.72, 1160.42)\" x=\"1825.72\" y=\"1160.42\">13</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 1070.4)\" x=\"191.754\" y=\"1070.4\">0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 915.659)\" x=\"191.754\" y=\"915.659\">1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 760.915)\" x=\"191.754\" y=\"760.915\">1.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 606.172)\" x=\"191.754\" y=\"606.172\">2.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 451.428)\" x=\"191.754\" y=\"451.428\">2.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 296.685)\" x=\"191.754\" y=\"296.685\">3.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 191.754, 141.941)\" x=\"191.754\" y=\"141.941\">3.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1041.85, 73.2)\" x=\"1041.85\" y=\"73.2\">Black-Scholes call option price</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1041.85, 1237.88)\" x=\"1041.85\" y=\"1237.88\">strike price</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4500)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 94.0861, 615.247)\" x=\"94.0861\" y=\"615.247\">option price</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip4502)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  257.986,149.613 289.341,181.311 320.696,212.617 352.05,243.511 383.405,273.97 414.76,303.974 446.115,333.504 477.469,362.541 508.824,391.068 540.179,419.067 \n",
       "  571.534,446.525 602.888,473.427 634.243,499.761 665.598,525.516 696.953,550.682 728.307,575.252 759.662,599.218 791.017,622.576 822.372,645.322 853.726,667.453 \n",
       "  885.081,688.968 916.436,709.867 947.791,730.151 979.145,749.824 1010.5,768.889 1041.85,787.351 1073.21,805.215 1104.56,822.488 1135.92,839.178 1167.27,855.294 \n",
       "  1198.63,870.843 1229.98,885.837 1261.34,900.285 1292.69,914.199 1324.05,927.589 1355.4,940.468 1386.76,952.848 1418.11,964.74 1449.47,976.159 1480.82,987.115 \n",
       "  1512.18,997.623 1543.53,1007.7 1574.89,1017.35 1606.24,1026.59 1637.6,1035.43 1668.95,1043.89 1700.3,1051.98 1731.66,1059.71 1763.01,1067.1 1794.37,1074.15 \n",
       "  1825.72,1080.88 \n",
       "  \"/>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot( K,c,\n",
    "      color = :red,\n",
    "      legend = false,\n",
    "      title = \"Black-Scholes call option price\",\n",
    "      xlabel = \"strike price\",\n",
    "      ylabel = \"option price\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Implied Volatility\n",
    "\n",
    "is the $\\sigma$ value that makes the Black-Scholes equation give the same option price as observed on the market. It is often interpreted as the \"market uncertainty.\"\n",
    "\n",
    "The next cell uses the call option price calculated above as the market price. The implied volatility should then equal the volatility used above (this is a way to check your coding).\n",
    "\n",
    "The next few cells instead use some data on options on German government bonds. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Implied volatility:      0.400, compare with: 0.4\n"
     ]
    }
   ],
   "source": [
    "                                #solve for implied vol\n",
    "iv = find_zero(σ->OptionBlackSPs(10,10,0.5,0.1,σ)-c1,(0.00001,5))\n",
    "\n",
    "printlnPs(\"Implied volatility: \",iv,\", compare with: $σ\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  LIFFE Bunds option data, trade date April 6, 1994\n",
    "K = [                        #strike prices; Mx1 vector\n",
    "      92.00;  94.00;  94.50;  95.00;  95.50;  96.00;  96.50;  97.00;\n",
    "      97.50;  98.00;  98.50;  99.00;  99.50;  100.0;  100.5;  101.0;\n",
    "     101.5;  102.0;  102.5;  103.0;  103.5 ];\n",
    "C = [                        #call prices; Mx1 vector\n",
    "      5.13;    3.25;    2.83;    2.40;    2.00;    1.64;    1.31;    1.02;\n",
    "      0.770;   0.570;   0.400;   0.280;   0.190;   0.130;  0.0800;  0.0500;\n",
    "      0.0400;  0.0300;  0.0200;  0.0100;  0.0100 ];\n",
    "S = 97.05                #spot price\n",
    "m = 48/365               #time to expiry in years\n",
    "y = 0.0                  #Interest rate: LIFFE=>no discounting\n",
    "N = length(K)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strike and iv for data: \n",
      "    92.000     0.094\n",
      "    94.000     0.081\n",
      "    94.500     0.081\n",
      "    95.000     0.078\n",
      "    95.500     0.075\n",
      "    96.000     0.074\n",
      "    96.500     0.072\n",
      "    97.000     0.071\n",
      "    97.500     0.070\n",
      "    98.000     0.069\n",
      "    98.500     0.068\n",
      "    99.000     0.067\n",
      "    99.500     0.067\n",
      "   100.000     0.068\n",
      "   100.500     0.067\n",
      "   101.000     0.067\n",
      "   101.500     0.070\n",
      "   102.000     0.073\n",
      "   102.500     0.074\n",
      "   103.000     0.072\n",
      "   103.500     0.077\n",
      "\n"
     ]
    }
   ],
   "source": [
    "iv = fill(NaN,N)       #looping over strikes\n",
    "for i = 1:N\n",
    "  iv[i] = find_zero(sigma->OptionBlackSPs(S,K[i],m,y,sigma)-C[i],(0.00001,5))\n",
    "end\n",
    "\n",
    "println(\"Strike and iv for data: \")\n",
    "printmat([K iv])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip4900\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4900)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4901\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip4900)\" d=\"\n",
       "M202.623 1108.82 L1872.76 1108.82 L1872.76 121.675 L202.623 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip4902\">\n",
       "    <rect x=\"202\" y=\"121\" width=\"1671\" height=\"988\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  318.395,1108.82 318.395,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  660.916,1108.82 660.916,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1003.44,1108.82 1003.44,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1345.96,1108.82 1345.96,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1688.48,1108.82 1688.48,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  202.623,971.06 1872.76,971.06 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  202.623,798.562 1872.76,798.562 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  202.623,626.064 1872.76,626.064 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  202.623,453.567 1872.76,453.567 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  202.623,281.069 1872.76,281.069 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,1108.82 202.623,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  318.395,1108.82 318.395,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  660.916,1108.82 660.916,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1003.44,1108.82 1003.44,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1345.96,1108.82 1345.96,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1688.48,1108.82 1688.48,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,971.06 222.664,971.06 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,798.562 222.664,798.562 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,626.064 222.664,626.064 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,453.567 222.664,453.567 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip4900)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  202.623,281.069 222.664,281.069 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 318.395, 1160.42)\" x=\"318.395\" y=\"1160.42\">92.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 660.916, 1160.42)\" x=\"660.916\" y=\"1160.42\">95.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1003.44, 1160.42)\" x=\"1003.44\" y=\"1160.42\">97.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1345.96, 1160.42)\" x=\"1345.96\" y=\"1160.42\">100.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1688.48, 1160.42)\" x=\"1688.48\" y=\"1160.42\">102.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 183.423, 988.56)\" x=\"183.423\" y=\"988.56\">0.070</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 183.423, 816.062)\" x=\"183.423\" y=\"816.062\">0.075</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 183.423, 643.564)\" x=\"183.423\" y=\"643.564\">0.080</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 183.423, 471.067)\" x=\"183.423\" y=\"471.067\">0.085</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 183.423, 298.569)\" x=\"183.423\" y=\"298.569\">0.090</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1037.69, 73.2)\" x=\"1037.69\" y=\"73.2\">Implied volatility</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1037.69, 1237.88)\" x=\"1037.69\" y=\"1237.88\">strike price</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip4902)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  249.891,149.613 523.908,580.198 592.412,588.415 660.916,705.968 729.42,790.985 797.924,838.667 866.429,893.988 934.933,940.947 1003.44,986.996 1071.94,1009.13 \n",
       "  1140.45,1056.82 1208.95,1064.1 1277.45,1071.34 1345.96,1051.91 1414.46,1080.88 1482.97,1080.19 1551.47,961.374 1619.98,877.826 1688.48,845.022 1756.98,907.358 \n",
       "  1825.49,733.504 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip4900)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 1071.94, 298.569)\" x=\"1071.94\" y=\"298.569\">Bunds options April 6, 1994</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot( K,iv,\n",
    "      color = :red,\n",
    "      legend =false,\n",
    "      title = \"Implied volatility\",\n",
    "      xlabel = \"strike price\",\n",
    "      annotation = (98,0.09,text(\"Bunds options April 6, 1994\",8,:left)) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mean-Variance Frontier\n",
    "\n",
    "Given a vector of average returns ($\\mu$) and a variance-covariance matrix ($\\Sigma$), the mean-variance frontier shows the lowest possible portfolio uncertainty for a given expected portfolio return (denoted $\\mu\\text{star}$ below).\n",
    "\n",
    "It is thus the solution to a quadratic minimization problem. The cells below will use the explicit (matrix) formulas for this solution, but we often have to resort to numerical methods when there are portfolio restrictions.\n",
    "\n",
    "It is typically plotted with the portfolio standard deviation on the horizontal axis and the portfolio expected return on the vertical axis.\n",
    "\n",
    "We calculate and plot two different mean-variance frontiers: (1) when we only consider risky assets; (2) when we also consider a risk-free asset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "μ: \n",
      "     0.115\n",
      "     0.095\n",
      "     0.060\n",
      "\n",
      "Σ: \n",
      "     0.017     0.003     0.006\n",
      "     0.003     0.006     0.000\n",
      "     0.006     0.000     0.010\n",
      "\n",
      "Rf: \n",
      "     0.030\n",
      "\n"
     ]
    }
   ],
   "source": [
    "μ = [11.5; 9.5; 6]/100          #expected returns\n",
    "Σ  = [166  34  58;              #covariance matrix\n",
    "       34  64   4;\n",
    "       58   4 100]/100^2\n",
    "Rf = 0.03                       #riskfree return (an interest rate)\n",
    "\n",
    "println(\"μ: \")\n",
    "printmat(μ)\n",
    "println(\"Σ: \")\n",
    "printmat(Σ)\n",
    "println(\"Rf: \")\n",
    "printmat(Rf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MVCalcRf (generic function with 1 method)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function MVCalc(μstar,μ,Σ)           #calculate the std of a portfolio on MVF of risky assets\n",
    "    n    = length(μ)                  #μstar is a scalar, μ a vector and Σ a matrix\n",
    "    oneV = ones(n) \n",
    "    Σ_1  = inv(Σ)\n",
    "    A    = μ'Σ_1*μ\n",
    "    B    = μ'Σ_1*oneV\n",
    "    C    = oneV'Σ_1*oneV\n",
    "    λ    = (C*μstar - B)/(A*C-B^2)\n",
    "    δ    = (A-B*μstar)/(A*C-B^2)\n",
    "    w    = Σ_1 *(μ*λ + δ*oneV)\n",
    "    StdRp = sqrt(w'Σ*w)\n",
    "    return StdRp,w\n",
    "end\n",
    "\n",
    "function MVCalcRf(μstar,μ,Σ,Rf)           #calculates the std of a portfolio on MVF of (Risky,Riskfree)\n",
    "    n     = length(μ)\n",
    "    μe    = μ .- Rf                        #expected excess returns\n",
    "    Σ_1   = inv(Σ)\n",
    "    w     = (μstar - Rf)/(μe'Σ_1*μe) * Σ_1 *μe\n",
    "    StdRp = sqrt(w'Σ*w)\n",
    "    return StdRp,w\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "μstar = range(Rf,stop=0.15,length=201)\n",
    "L     = length(μstar)\n",
    "\n",
    "StdRp  = fill(NaN,L)                      #risky assets only \n",
    "for i = 1:L                               #loop over different expected portfolio returns\n",
    "    StdRp[i] = MVCalc(μstar[i],μ,Σ)[1]    #[1] to get the first output\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip5300\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip5300)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip5301\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip5300)\" d=\"\n",
       "M197.573 1108.82 L1872.76 1108.82 L1872.76 121.675 L197.573 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip5302\">\n",
       "    <rect x=\"197\" y=\"121\" width=\"1676\" height=\"988\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,779.771 1872.76,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,450.723 1872.76,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,121.675 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 217.675,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,779.771 217.675,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,450.723 217.675,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,121.675 217.675,121.675 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 197.573, 1160.42)\" x=\"197.573\" y=\"1160.42\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 755.967, 1160.42)\" x=\"755.967\" y=\"1160.42\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1314.36, 1160.42)\" x=\"1314.36\" y=\"1160.42\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1872.76, 1160.42)\" x=\"1872.76\" y=\"1160.42\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 1126.32)\" x=\"178.373\" y=\"1126.32\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 797.271)\" x=\"178.373\" y=\"797.271\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 468.223)\" x=\"178.373\" y=\"468.223\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 139.175)\" x=\"178.373\" y=\"139.175\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 73.2)\" x=\"1035.16\" y=\"73.2\">MVF, only risky assets</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 1237.88)\" x=\"1035.16\" y=\"1237.88\">Std(Rp), %</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 94.0861, 615.247)\" x=\"94.0861\" y=\"615.247\">ERp, %</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip5302)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1631.89,911.39 1619.16,907.441 1606.47,903.493 1593.81,899.544 1581.2,895.596 1568.63,891.647 1556.1,887.698 1543.62,883.75 1531.18,879.801 1518.79,875.853 \n",
       "  1506.44,871.904 1494.14,867.956 1481.9,864.007 1469.71,860.058 1457.57,856.11 1445.48,852.161 1433.45,848.213 1421.48,844.264 1409.57,840.316 1397.73,836.367 \n",
       "  1385.94,832.418 1374.22,828.47 1362.57,824.521 1350.99,820.573 1339.48,816.624 1328.05,812.676 1316.69,808.727 1305.41,804.778 1294.21,800.83 1283.09,796.881 \n",
       "  1272.06,792.933 1261.12,788.984 1250.28,785.036 1239.52,781.087 1228.86,777.138 1218.31,773.19 1207.85,769.241 1197.51,765.293 1187.27,761.344 1177.15,757.396 \n",
       "  1167.14,753.447 1157.26,749.498 1147.5,745.55 1137.87,741.601 1128.37,737.653 1119.01,733.704 1109.79,729.756 1100.71,725.807 1091.79,721.858 1083.01,717.91 \n",
       "  1074.4,713.961 1065.95,710.013 1057.67,706.064 1049.56,702.116 1041.63,698.167 1033.88,694.218 1026.32,690.27 1018.96,686.321 1011.79,682.373 1004.83,678.424 \n",
       "  998.077,674.475 991.539,670.527 985.221,666.578 979.129,662.63 973.267,658.681 967.642,654.733 962.258,650.784 957.12,646.835 952.233,642.887 947.603,638.938 \n",
       "  943.235,634.99 939.131,631.041 935.299,627.093 931.74,623.144 928.46,619.195 925.462,615.247 922.75,611.298 920.326,607.35 918.195,603.401 916.358,599.453 \n",
       "  914.817,595.504 913.575,591.555 912.634,587.607 911.993,583.658 911.655,579.71 911.619,575.761 911.886,571.813 912.455,567.864 913.326,563.915 914.497,559.967 \n",
       "  915.968,556.018 917.735,552.07 919.797,548.121 922.152,544.173 924.796,540.224 927.727,536.275 930.941,532.327 934.434,528.378 938.203,524.43 942.243,520.481 \n",
       "  946.549,516.533 951.118,512.584 955.945,508.635 961.024,504.687 966.35,500.738 971.919,496.79 977.725,492.841 983.764,488.893 990.029,484.944 996.516,480.995 \n",
       "  1003.22,477.047 1010.13,473.098 1017.25,469.15 1024.57,465.201 1032.08,461.252 1039.79,457.304 1047.67,453.355 1055.74,449.407 1063.98,445.458 1072.39,441.51 \n",
       "  1080.97,437.561 1089.7,433.612 1098.59,429.664 1107.63,425.715 1116.82,421.767 1126.15,417.818 1135.62,413.87 1145.22,409.921 1154.95,405.972 1164.8,402.024 \n",
       "  1174.78,398.075 1184.87,394.127 1195.08,390.178 1205.4,386.23 1215.83,382.281 1226.36,378.332 1237,374.384 1247.73,370.435 1258.56,366.487 1269.48,362.538 \n",
       "  1280.49,358.59 1291.58,354.641 1302.76,350.692 1314.02,346.744 1325.36,342.795 1336.78,338.847 1348.27,334.898 1359.84,330.95 1371.47,327.001 1383.17,323.052 \n",
       "  1394.94,319.104 1406.77,315.155 1418.67,311.207 1430.63,307.258 1442.64,303.31 1454.71,299.361 1466.84,295.412 1479.02,291.464 1491.25,287.515 1503.54,283.567 \n",
       "  1515.87,279.618 1528.25,275.67 1540.68,271.721 1553.15,267.772 1565.67,263.824 1578.23,259.875 1590.84,255.927 1603.48,251.978 1616.16,248.03 1628.89,244.081 \n",
       "  1641.65,240.132 1654.44,236.184 1667.27,232.235 1680.14,228.287 1693.04,224.338 1705.97,220.389 1718.94,216.441 1731.94,212.492 1744.96,208.544 1758.02,204.595 \n",
       "  1771.11,200.647 1784.22,196.698 1797.36,192.749 1810.53,188.801 1823.73,184.852 1836.95,180.904 1850.19,176.955 1863.46,173.007 1876.76,169.058 1890.07,165.109 \n",
       "  1903.41,161.161 1916.78,157.212 1930.16,153.264 1943.56,149.315 1956.99,145.367 1970.44,141.418 1983.9,137.469 1997.39,133.521 2010.89,129.572 2024.41,125.624 \n",
       "  2037.95,121.675 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5302)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"14\"/>\n",
       "<path clip-path=\"url(#clip5300)\" d=\"\n",
       "M255.173 357.835 L586.279 357.835 L586.279 176.395 L255.173 176.395  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  255.173,357.835 586.279,357.835 586.279,176.395 255.173,176.395 255.173,357.835 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5300)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  274.373,236.875 389.573,236.875 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 408.773, 254.375)\" x=\"408.773\" y=\"254.375\">MVF</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip5300)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"341.573\" cy=\"297.355\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip5300)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"341.573\" cy=\"297.355\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip5300)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 408.773, 314.855)\" x=\"408.773\" y=\"314.855\">assets</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot( StdRp*100,μstar*100,             \n",
    "      linecolor = :red,\n",
    "      xlim = (0,15),\n",
    "      ylim = (0,15),\n",
    "      label = \"MVF\",\n",
    "      legend = :topleft,\n",
    "      title = \"MVF, only risky assets\",\n",
    "      xlabel = \"Std(Rp), %\",\n",
    "      ylabel = \"ERp, %\" )\n",
    "\n",
    "scatter!(sqrt.(diag(Σ))*100,μ*100,color=:red,label=\"assets\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "StdRpRf  = fill(NaN,L)       #with riskfree too\n",
    "for i = 1:L\n",
    "    StdRpRf[i] = MVCalcRf(μstar[i],μ,Σ,Rf)[1]\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip5700\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip5700)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip5701\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip5700)\" d=\"\n",
       "M197.573 1108.82 L1872.76 1108.82 L1872.76 121.675 L197.573 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip5702\">\n",
       "    <rect x=\"197\" y=\"121\" width=\"1676\" height=\"988\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,779.771 1872.76,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,450.723 1872.76,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,121.675 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 217.675,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,779.771 217.675,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,450.723 217.675,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,121.675 217.675,121.675 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 197.573, 1160.42)\" x=\"197.573\" y=\"1160.42\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 755.967, 1160.42)\" x=\"755.967\" y=\"1160.42\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1314.36, 1160.42)\" x=\"1314.36\" y=\"1160.42\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1872.76, 1160.42)\" x=\"1872.76\" y=\"1160.42\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 1126.32)\" x=\"178.373\" y=\"1126.32\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 797.271)\" x=\"178.373\" y=\"797.271\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 468.223)\" x=\"178.373\" y=\"468.223\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 139.175)\" x=\"178.373\" y=\"139.175\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 73.2)\" x=\"1035.16\" y=\"73.2\">MVF, risky and riskfree assets</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 1237.88)\" x=\"1035.16\" y=\"1237.88\">Std(Rp), %</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip5700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 94.0861, 615.247)\" x=\"94.0861\" y=\"615.247\">ERp, %</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1631.89,911.39 1619.16,907.441 1606.47,903.493 1593.81,899.544 1581.2,895.596 1568.63,891.647 1556.1,887.698 1543.62,883.75 1531.18,879.801 1518.79,875.853 \n",
       "  1506.44,871.904 1494.14,867.956 1481.9,864.007 1469.71,860.058 1457.57,856.11 1445.48,852.161 1433.45,848.213 1421.48,844.264 1409.57,840.316 1397.73,836.367 \n",
       "  1385.94,832.418 1374.22,828.47 1362.57,824.521 1350.99,820.573 1339.48,816.624 1328.05,812.676 1316.69,808.727 1305.41,804.778 1294.21,800.83 1283.09,796.881 \n",
       "  1272.06,792.933 1261.12,788.984 1250.28,785.036 1239.52,781.087 1228.86,777.138 1218.31,773.19 1207.85,769.241 1197.51,765.293 1187.27,761.344 1177.15,757.396 \n",
       "  1167.14,753.447 1157.26,749.498 1147.5,745.55 1137.87,741.601 1128.37,737.653 1119.01,733.704 1109.79,729.756 1100.71,725.807 1091.79,721.858 1083.01,717.91 \n",
       "  1074.4,713.961 1065.95,710.013 1057.67,706.064 1049.56,702.116 1041.63,698.167 1033.88,694.218 1026.32,690.27 1018.96,686.321 1011.79,682.373 1004.83,678.424 \n",
       "  998.077,674.475 991.539,670.527 985.221,666.578 979.129,662.63 973.267,658.681 967.642,654.733 962.258,650.784 957.12,646.835 952.233,642.887 947.603,638.938 \n",
       "  943.235,634.99 939.131,631.041 935.299,627.093 931.74,623.144 928.46,619.195 925.462,615.247 922.75,611.298 920.326,607.35 918.195,603.401 916.358,599.453 \n",
       "  914.817,595.504 913.575,591.555 912.634,587.607 911.993,583.658 911.655,579.71 911.619,575.761 911.886,571.813 912.455,567.864 913.326,563.915 914.497,559.967 \n",
       "  915.968,556.018 917.735,552.07 919.797,548.121 922.152,544.173 924.796,540.224 927.727,536.275 930.941,532.327 934.434,528.378 938.203,524.43 942.243,520.481 \n",
       "  946.549,516.533 951.118,512.584 955.945,508.635 961.024,504.687 966.35,500.738 971.919,496.79 977.725,492.841 983.764,488.893 990.029,484.944 996.516,480.995 \n",
       "  1003.22,477.047 1010.13,473.098 1017.25,469.15 1024.57,465.201 1032.08,461.252 1039.79,457.304 1047.67,453.355 1055.74,449.407 1063.98,445.458 1072.39,441.51 \n",
       "  1080.97,437.561 1089.7,433.612 1098.59,429.664 1107.63,425.715 1116.82,421.767 1126.15,417.818 1135.62,413.87 1145.22,409.921 1154.95,405.972 1164.8,402.024 \n",
       "  1174.78,398.075 1184.87,394.127 1195.08,390.178 1205.4,386.23 1215.83,382.281 1226.36,378.332 1237,374.384 1247.73,370.435 1258.56,366.487 1269.48,362.538 \n",
       "  1280.49,358.59 1291.58,354.641 1302.76,350.692 1314.02,346.744 1325.36,342.795 1336.78,338.847 1348.27,334.898 1359.84,330.95 1371.47,327.001 1383.17,323.052 \n",
       "  1394.94,319.104 1406.77,315.155 1418.67,311.207 1430.63,307.258 1442.64,303.31 1454.71,299.361 1466.84,295.412 1479.02,291.464 1491.25,287.515 1503.54,283.567 \n",
       "  1515.87,279.618 1528.25,275.67 1540.68,271.721 1553.15,267.772 1565.67,263.824 1578.23,259.875 1590.84,255.927 1603.48,251.978 1616.16,248.03 1628.89,244.081 \n",
       "  1641.65,240.132 1654.44,236.184 1667.27,232.235 1680.14,228.287 1693.04,224.338 1705.97,220.389 1718.94,216.441 1731.94,212.492 1744.96,208.544 1758.02,204.595 \n",
       "  1771.11,200.647 1784.22,196.698 1797.36,192.749 1810.53,188.801 1823.73,184.852 1836.95,180.904 1850.19,176.955 1863.46,173.007 1876.76,169.058 1890.07,165.109 \n",
       "  1903.41,161.161 1916.78,157.212 1930.16,153.264 1943.56,149.315 1956.99,145.367 1970.44,141.418 1983.9,137.469 1997.39,133.521 2010.89,129.572 2024.41,125.624 \n",
       "  2037.95,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip5702)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" stroke-dasharray=\"16, 10\" points=\"\n",
       "  197.573,911.39 204.891,907.441 212.21,903.493 219.528,899.544 226.846,895.596 234.165,891.647 241.483,887.698 248.801,883.75 256.12,879.801 263.438,875.853 \n",
       "  270.756,871.904 278.075,867.956 285.393,864.007 292.711,860.058 300.029,856.11 307.348,852.161 314.666,848.213 321.984,844.264 329.303,840.316 336.621,836.367 \n",
       "  343.939,832.418 351.258,828.47 358.576,824.521 365.894,820.573 373.213,816.624 380.531,812.676 387.849,808.727 395.168,804.778 402.486,800.83 409.804,796.881 \n",
       "  417.123,792.933 424.441,788.984 431.759,785.036 439.078,781.087 446.396,777.138 453.714,773.19 461.033,769.241 468.351,765.293 475.669,761.344 482.988,757.396 \n",
       "  490.306,753.447 497.624,749.498 504.943,745.55 512.261,741.601 519.579,737.653 526.898,733.704 534.216,729.756 541.534,725.807 548.853,721.858 556.171,717.91 \n",
       "  563.489,713.961 570.808,710.013 578.126,706.064 585.444,702.116 592.763,698.167 600.081,694.218 607.399,690.27 614.718,686.321 622.036,682.373 629.354,678.424 \n",
       "  636.673,674.475 643.991,670.527 651.309,666.578 658.628,662.63 665.946,658.681 673.264,654.733 680.583,650.784 687.901,646.835 695.219,642.887 702.538,638.938 \n",
       "  709.856,634.99 717.174,631.041 724.492,627.093 731.811,623.144 739.129,619.195 746.447,615.247 753.766,611.298 761.084,607.35 768.402,603.401 775.721,599.453 \n",
       "  783.039,595.504 790.357,591.555 797.676,587.607 804.994,583.658 812.312,579.71 819.631,575.761 826.949,571.813 834.267,567.864 841.586,563.915 848.904,559.967 \n",
       "  856.222,556.018 863.541,552.07 870.859,548.121 878.177,544.173 885.496,540.224 892.814,536.275 900.132,532.327 907.451,528.378 914.769,524.43 922.087,520.481 \n",
       "  929.406,516.533 936.724,512.584 944.042,508.635 951.361,504.687 958.679,500.738 965.997,496.79 973.316,492.841 980.634,488.893 987.952,484.944 995.271,480.995 \n",
       "  1002.59,477.047 1009.91,473.098 1017.23,469.15 1024.54,465.201 1031.86,461.252 1039.18,457.304 1046.5,453.355 1053.82,449.407 1061.14,445.458 1068.45,441.51 \n",
       "  1075.77,437.561 1083.09,433.612 1090.41,429.664 1097.73,425.715 1105.05,421.767 1112.36,417.818 1119.68,413.87 1127,409.921 1134.32,405.972 1141.64,402.024 \n",
       "  1148.96,398.075 1156.27,394.127 1163.59,390.178 1170.91,386.23 1178.23,382.281 1185.55,378.332 1192.87,374.384 1200.18,370.435 1207.5,366.487 1214.82,362.538 \n",
       "  1222.14,358.59 1229.46,354.641 1236.78,350.692 1244.09,346.744 1251.41,342.795 1258.73,338.847 1266.05,334.898 1273.37,330.95 1280.69,327.001 1288,323.052 \n",
       "  1295.32,319.104 1302.64,315.155 1309.96,311.207 1317.28,307.258 1324.6,303.31 1331.91,299.361 1339.23,295.412 1346.55,291.464 1353.87,287.515 1361.19,283.567 \n",
       "  1368.51,279.618 1375.82,275.67 1383.14,271.721 1390.46,267.772 1397.78,263.824 1405.1,259.875 1412.42,255.927 1419.73,251.978 1427.05,248.03 1434.37,244.081 \n",
       "  1441.69,240.132 1449.01,236.184 1456.33,232.235 1463.64,228.287 1470.96,224.338 1478.28,220.389 1485.6,216.441 1492.92,212.492 1500.24,208.544 1507.55,204.595 \n",
       "  1514.87,200.647 1522.19,196.698 1529.51,192.749 1536.83,188.801 1544.15,184.852 1551.46,180.904 1558.78,176.955 1566.1,173.007 1573.42,169.058 1580.74,165.109 \n",
       "  1588.06,161.161 1595.37,157.212 1602.69,153.264 1610.01,149.315 1617.33,145.367 1624.65,141.418 1631.97,137.469 1639.28,133.521 1646.6,129.572 1653.92,125.624 \n",
       "  1661.24,121.675 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip5702)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"14\"/>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot( [StdRp StdRpRf]*100,[μstar μstar]*100,\n",
    "      legend = nothing,\n",
    "      linestyle = [:solid :dash],\n",
    "      linecolor = [:red :blue],\n",
    "      xlim = (0,15),\n",
    "      ylim = (0,15), \n",
    "      title = \"MVF, risky and riskfree assets\",\n",
    "      xlabel = \"Std(Rp), %\",\n",
    "      ylabel = \"ERp, %\" )\n",
    "\n",
    "scatter!(sqrt.(diag(Σ))*100,μ*100,color=:red)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mean-Variance Frontier without Short Selling (extra)\n",
    "\n",
    "The code below solves (numerically) the following minimization problem \n",
    "\n",
    "$\\min \\text{Var}(R_p) \\: \\text{ s.t. } \\: \\text{E}R_p = \\mu^*$,\n",
    " \n",
    "and where we also require $w_i\\ge 0$ and $\\sum_{i=1}^{n}w_{i}=1$.\n",
    "\n",
    "To solve this, we use the [JuMP](https://github.com/JuliaOpt/JuMP.jl) package (version >= 0.19) together with [Ipopt](https://github.com/JuliaOpt/Ipopt.jl)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "using JuMP, Ipopt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MeanVarNoSSPs (generic function with 1 method)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function MeanVarNoSSPs(μ,Σ,μstar)   #MV with no short-sales, numerical minimization\n",
    "    n = length(μ)\n",
    "    if minimum(μ) <= μstar <= maximum(μ)      #try only if feasible\n",
    "        model = Model(with_optimizer(Ipopt.Optimizer, print_level = 1))\n",
    "        @variable(model,w[i=1:n] >= 0.0)       #no short sales\n",
    "        @objective(model,Min,w'*Σ*w)           #minimize portfolio variance \n",
    "        @constraint(model,sum(w) == 1.0)       #w sums to 1\n",
    "        @constraint(model,w'*μ == μstar)       #mean equals μstar\n",
    "        optimize!(model)\n",
    "        if has_values(model)\n",
    "            w_p = value.(w)\n",
    "            StdRp = sqrt(w_p'Σ*w_p)\n",
    "        end\n",
    "    else\n",
    "        (w_p,StdRp) = (NaN,NaN)\n",
    "    end\n",
    "    return StdRp,w_p\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "******************************************************************************\n",
      "This program contains Ipopt, a library for large-scale nonlinear optimization.\n",
      " Ipopt is released as open source code under the Eclipse Public License (EPL).\n",
      "         For more information visit http://projects.coin-or.org/Ipopt\n",
      "******************************************************************************\n",
      "\n"
     ]
    }
   ],
   "source": [
    "Std_no_ss = fill(NaN,length(μstar))\n",
    "for i = 1:length(μstar)                   #risky assets only, no short sales\n",
    "    Std_no_ss[i] = MeanVarNoSSPs(μ,Σ,μstar[i])[1]\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip6100\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip6100)\" d=\"\n",
       "M0 1280 L1920 1280 L1920 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip6101\">\n",
       "    <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip6100)\" d=\"\n",
       "M197.573 1108.82 L1872.76 1108.82 L1872.76 121.675 L197.573 121.675  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip6102\">\n",
       "    <rect x=\"197\" y=\"121\" width=\"1676\" height=\"988\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,779.771 1872.76,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,450.723 1872.76,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  197.573,121.675 1872.76,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 1872.76,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 197.573,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  755.967,1108.82 755.967,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1314.36,1108.82 1314.36,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1872.76,1108.82 1872.76,1096.97 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,1108.82 217.675,1108.82 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,779.771 217.675,779.771 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,450.723 217.675,450.723 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  197.573,121.675 217.675,121.675 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 197.573, 1160.42)\" x=\"197.573\" y=\"1160.42\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 755.967, 1160.42)\" x=\"755.967\" y=\"1160.42\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1314.36, 1160.42)\" x=\"1314.36\" y=\"1160.42\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1872.76, 1160.42)\" x=\"1872.76\" y=\"1160.42\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 1126.32)\" x=\"178.373\" y=\"1126.32\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 797.271)\" x=\"178.373\" y=\"797.271\">5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 468.223)\" x=\"178.373\" y=\"468.223\">10</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 178.373, 139.175)\" x=\"178.373\" y=\"139.175\">15</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:84px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 73.2)\" x=\"1035.16\" y=\"73.2\">MVF (with/without constraints)</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1035.16, 1237.88)\" x=\"1035.16\" y=\"1237.88\">Std(Rp), %</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 94.0861, 615.247)\" x=\"94.0861\" y=\"615.247\">ERp, %</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#ff0000; stroke-width:8; stroke-opacity:1; fill:none\" points=\"\n",
       "  1631.89,911.39 1619.16,907.441 1606.47,903.493 1593.81,899.544 1581.2,895.596 1568.63,891.647 1556.1,887.698 1543.62,883.75 1531.18,879.801 1518.79,875.853 \n",
       "  1506.44,871.904 1494.14,867.956 1481.9,864.007 1469.71,860.058 1457.57,856.11 1445.48,852.161 1433.45,848.213 1421.48,844.264 1409.57,840.316 1397.73,836.367 \n",
       "  1385.94,832.418 1374.22,828.47 1362.57,824.521 1350.99,820.573 1339.48,816.624 1328.05,812.676 1316.69,808.727 1305.41,804.778 1294.21,800.83 1283.09,796.881 \n",
       "  1272.06,792.933 1261.12,788.984 1250.28,785.036 1239.52,781.087 1228.86,777.138 1218.31,773.19 1207.85,769.241 1197.51,765.293 1187.27,761.344 1177.15,757.396 \n",
       "  1167.14,753.447 1157.26,749.498 1147.5,745.55 1137.87,741.601 1128.37,737.653 1119.01,733.704 1109.79,729.756 1100.71,725.807 1091.79,721.858 1083.01,717.91 \n",
       "  1074.4,713.961 1065.95,710.013 1057.67,706.064 1049.56,702.116 1041.63,698.167 1033.88,694.218 1026.32,690.27 1018.96,686.321 1011.79,682.373 1004.83,678.424 \n",
       "  998.077,674.475 991.539,670.527 985.221,666.578 979.129,662.63 973.267,658.681 967.642,654.733 962.258,650.784 957.12,646.835 952.233,642.887 947.603,638.938 \n",
       "  943.235,634.99 939.131,631.041 935.299,627.093 931.74,623.144 928.46,619.195 925.462,615.247 922.75,611.298 920.326,607.35 918.195,603.401 916.358,599.453 \n",
       "  914.817,595.504 913.575,591.555 912.634,587.607 911.993,583.658 911.655,579.71 911.619,575.761 911.886,571.813 912.455,567.864 913.326,563.915 914.497,559.967 \n",
       "  915.968,556.018 917.735,552.07 919.797,548.121 922.152,544.173 924.796,540.224 927.727,536.275 930.941,532.327 934.434,528.378 938.203,524.43 942.243,520.481 \n",
       "  946.549,516.533 951.118,512.584 955.945,508.635 961.024,504.687 966.35,500.738 971.919,496.79 977.725,492.841 983.764,488.893 990.029,484.944 996.516,480.995 \n",
       "  1003.22,477.047 1010.13,473.098 1017.25,469.15 1024.57,465.201 1032.08,461.252 1039.79,457.304 1047.67,453.355 1055.74,449.407 1063.98,445.458 1072.39,441.51 \n",
       "  1080.97,437.561 1089.7,433.612 1098.59,429.664 1107.63,425.715 1116.82,421.767 1126.15,417.818 1135.62,413.87 1145.22,409.921 1154.95,405.972 1164.8,402.024 \n",
       "  1174.78,398.075 1184.87,394.127 1195.08,390.178 1205.4,386.23 1215.83,382.281 1226.36,378.332 1237,374.384 1247.73,370.435 1258.56,366.487 1269.48,362.538 \n",
       "  1280.49,358.59 1291.58,354.641 1302.76,350.692 1314.02,346.744 1325.36,342.795 1336.78,338.847 1348.27,334.898 1359.84,330.95 1371.47,327.001 1383.17,323.052 \n",
       "  1394.94,319.104 1406.77,315.155 1418.67,311.207 1430.63,307.258 1442.64,303.31 1454.71,299.361 1466.84,295.412 1479.02,291.464 1491.25,287.515 1503.54,283.567 \n",
       "  1515.87,279.618 1528.25,275.67 1540.68,271.721 1553.15,267.772 1565.67,263.824 1578.23,259.875 1590.84,255.927 1603.48,251.978 1616.16,248.03 1628.89,244.081 \n",
       "  1641.65,240.132 1654.44,236.184 1667.27,232.235 1680.14,228.287 1693.04,224.338 1705.97,220.389 1718.94,216.441 1731.94,212.492 1744.96,208.544 1758.02,204.595 \n",
       "  1771.11,200.647 1784.22,196.698 1797.36,192.749 1810.53,188.801 1823.73,184.852 1836.95,180.904 1850.19,176.955 1863.46,173.007 1876.76,169.058 1890.07,165.109 \n",
       "  1903.41,161.161 1916.78,157.212 1930.16,153.264 1943.56,149.315 1956.99,145.367 1970.44,141.418 1983.9,137.469 1997.39,133.521 2010.89,129.572 2024.41,125.624 \n",
       "  2037.95,121.675 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6102)\" style=\"stroke:#008000; stroke-width:8; stroke-opacity:1; fill:none\" stroke-dasharray=\"32, 20\" points=\"\n",
       "  1314.36,713.961 1296.09,710.013 1278.04,706.064 1260.22,702.116 1242.64,698.167 1225.32,694.218 1208.26,690.27 1191.49,686.321 1175.02,682.373 1158.86,678.424 \n",
       "  1143.03,674.475 1127.54,670.527 1112.42,666.578 1097.67,662.63 1083.33,658.681 1069.41,654.733 1055.93,650.784 1042.91,646.835 1030.37,642.887 1018.34,638.938 \n",
       "  1006.84,634.99 995.883,631.041 985.503,627.093 975.72,623.144 966.556,619.195 958.034,615.247 950.176,611.298 943.002,607.35 936.533,603.401 930.787,599.453 \n",
       "  925.781,595.504 921.53,591.555 918.049,587.607 915.347,583.658 913.435,579.71 912.318,575.761 912,571.813 912.455,567.864 913.326,563.915 914.497,559.967 \n",
       "  915.968,556.018 917.735,552.07 919.797,548.121 922.152,544.173 924.796,540.224 927.727,536.275 930.941,532.327 934.434,528.378 938.203,524.43 942.243,520.481 \n",
       "  946.549,516.533 951.118,512.584 955.945,508.635 961.024,504.687 966.35,500.738 971.919,496.79 977.725,492.841 983.764,488.893 990.029,484.944 996.516,480.995 \n",
       "  1003.22,477.047 1010.13,473.098 1017.25,469.15 1024.57,465.201 1032.08,461.252 1039.79,457.304 1047.67,453.355 1055.74,449.407 1063.98,445.458 1072.59,441.51 \n",
       "  1082.91,437.561 1095.13,433.612 1109.19,429.664 1125,425.715 1142.47,421.767 1161.51,417.818 1182.03,413.87 1203.94,409.921 1227.15,405.972 1251.58,402.024 \n",
       "  1277.14,398.075 1303.75,394.127 1331.34,390.178 1359.84,386.23 1389.19,382.281 1419.32,378.332 1450.18,374.384 1481.71,370.435 1513.87,366.487 1546.61,362.538 \n",
       "  1579.89,358.59 1613.67,354.641 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1636.45\" cy=\"352.009\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1091\" cy=\"483.628\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip6102)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"1314.36\" cy=\"713.961\" r=\"14\"/>\n",
       "<path clip-path=\"url(#clip6100)\" d=\"\n",
       "M255.173 418.315 L746.805 418.315 L746.805 176.395 L255.173 176.395  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  255.173,418.315 746.805,418.315 746.805,176.395 255.173,176.395 255.173,418.315 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#ff0000; stroke-width:8; stroke-opacity:1; fill:none\" points=\"\n",
       "  274.373,236.875 389.573,236.875 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 408.773, 254.375)\" x=\"408.773\" y=\"254.375\">no constraints</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip6100)\" style=\"stroke:#008000; stroke-width:8; stroke-opacity:1; fill:none\" stroke-dasharray=\"32, 20\" points=\"\n",
       "  274.373,297.355 389.573,297.355 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 408.773, 314.855)\" x=\"408.773\" y=\"314.855\">no short sales</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip6100)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"341.573\" cy=\"357.835\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip6100)\" style=\"fill:#ff0000; stroke:none; fill-opacity:1\" cx=\"341.573\" cy=\"357.835\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip6100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 408.773, 375.335)\" x=\"408.773\" y=\"375.335\">assets</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot( [StdRp Std_no_ss]*100,[μstar μstar]*100,\n",
    "      linecolor = [:red :green],\n",
    "      linestyle = [:solid :dash],\n",
    "      linewidth = 2, \n",
    "      label = [\"no constraints\" \"no short sales\"],\n",
    "      xlim = (0,15),\n",
    "      ylim = (0,15),\n",
    "      legend = :topleft,\n",
    "      title = \"MVF (with/without constraints)\",\n",
    "      xlabel = \"Std(Rp), %\",\n",
    "      ylabel = \"ERp, %\" )\n",
    "\n",
    "scatter!(sqrt.(diag(Σ))*100,μ*100,color=:red,label=\"assets\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.3.1",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
